<?php

App::uses('AppController', 'Controller');

class ReportsController extends AppController {
	
	private function _setCommonData() {
		
		$months = array(
			1 => __('January'),
			2 => __('February'),
			3 => __('March'),
			4 => __('April'),
			5 => __('May'),
			6 => __('June'),
			7 => __('July'),
			8 => __('August'),
			9 => __('September'),
			10 => __('October'),
			11 => __('November'),
			12 => __('December'),
		);
		
		$years = array();
		$year = (int)date('Y');
		for($i=$year-5; $i<$year+5; $i++){
			$years[$i] = $i;
		}
		
		$this->set(compact('months', 'years'));
	}
	
	public function index() {
	    $this->loadModel('Member');
		$this->_setCommonData();
		
		$filter = @$this->data['Filter'];
		if(empty($filter)) {
			$filter = array(
				'start' => array('month'=>1, 'year'=>date('Y')),
				'end' => array('month'=>date('m'), 'year'=>date('Y'))
			);
		}
		
		$start = $filter['start']['year']."-".$filter['start']['month']."-01";
		$end = end_of_month($filter['end']['year']."-".$filter['end']['month']."-01");
		
		$data = $this->Member->find('all', array(
			'fields' => array(
				'Member.id', 'Member.firstname', 'Member.surname', 'Member.voice',
				'Activity.id', 'Activity.type', 'Activity.date'
			),
			'conditions' => array(
				'Activity.date >=' => $start,
				'Activity.date <=' => $end
			),
			'recursive' => 0,
			'joins' => array(
				array(
					'table' => 'members_activities',
		            'alias' => 'MemberActivity',
		            'type' => 'INNER',
		            'conditions' => array(
		                'MemberActivity.member_id = Member.id'
		            )
				),
				array(
					'table' => 'activities',
		            'alias' => 'Activity',
		            'type' => 'INNER',
		            'conditions' => array(
		                'MemberActivity.activity_id = Activity.id'
		            )
				)
			),
			'order' => 'Member.voice, Activity.date'
		));
		$report = array();
		$members = array();
		$total_member = array();
		$total = array();
		foreach($data as $row) {
			$member_id = $row['Member']['id'];
			$members[$member_id] = $row['Member'];
						
			$activity_date = $row['Activity']['date'];
			$year  = (int)date("Y", strtotime($activity_date));
			$month = (int)date("m", strtotime($activity_date));
			if(!isset($report[$year])) {
				$report[$year] = array();
			}
			if(!isset($report[$year][$month])) {
				$report[$year][$month] = array(
					'Rehearsal' => array(),
					'Liturgy' => array(),
					'Rite' => array()
				);
			}
			
			$activity_type = $row['Activity']['type'];
			
			// increment total
			if(!isset($total[$year])) $total[$year] = array();			
			if(!isset($total[$year][$month])) $total[$year][$month] = array();			
			if(!isset($total[$year][$month][$activity_type])) $total[$year][$month][$activity_type] = 0;
			$total[$year][$month][$activity_type]++;
			
			// increment member activity type for month
			if(!isset($report[$year][$month][$activity_type][$member_id])){
				$report[$year][$month][$activity_type][$member_id] = 0;
			}
			$report[$year][$month][$activity_type][$member_id]++;
			
			// increment member activity type total
			if(!isset($total_member[$member_id])) {
				$total_member[$member_id] = array();
			}
			if(empty($total_member[$member_id][$activity_type])) {
				$total_member[$member_id][$activity_type] = 0;
			}
			$total_member[$member_id][$activity_type]++;
		}
		
		$this->set(compact('filter', 'report', 'members', 'total_member', 'total'));
	}
	
	public function traffic() {
	    $this->loadModel('Finance');
        $this->loadModel('FinanceCycle');
        
	    $filter = $this->data['Filter'];
	    
        if (! empty($filter)) {
            $lastCycle = $this->FinanceCycle->getLast($filter['start']);
            $initState = $this->Finance->getInitState($lastCycle, $filter['start']);
            
            $data      = $this->Finance->get($filter['start'], $filter['end'], $filter['type']);
        }
        
        $this->set(compact('initState', 'data', 'filter'));
	}
	
}

?>